package com.smartthinking.CICPNL.home.activity;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Dictionary;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.MarginLayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.smartthinking.CICPNL.R;
import com.smartthinking.CICPNL.R.id;
import com.smartthinking.CICPNL.home.objects.App_ImageLoader;
import com.smartthinking.CICPNL.home.objects.clsImagesShow;
import com.smartthinking.CICPNL.home.xml.saxXML;

public class Informacion extends Activity {

	protected Intent intent;
	private ArrayList<Dictionary> Actividades;

	private String informacion;
	private TextView tvInformacion;

	private String fotos;
	public static ArrayList<Dictionary> TraeDatos = null;

	private String ubicacion;
	private TextView tvUbicacion;

	private String contacto;
	private TextView tvContacto;
	public Gallery gallery1;
	public com.smartthinking.CICPNL.home.activity.Informacion.Hoteles_Adapter myAdapter;
	public App_ImageLoader imageLoader;

	private TextView descripcion;
	private TextView contactotelefono;
	private TextView contactomail;
	private TextView tvContactoMail;
	public int EVEIDR;

	public void abrirMapa(View v) {
		Intent intent = new Intent(Informacion.this, Mapa.class);

		// intent.putExtra("PUTLATITUDE", value);
		// intent.putExtra("PUTLONGITUDE", value);
		//

		startActivity(intent);

	}

// Agregar WWW
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);

		// Checks the orientation of the screen
//		if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
//			setContentView(R.layout.ui_informacion);
//		} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
//			setContentView(R.layout.ui_informacion);
//		}
	}
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.ui_informacion);

	/********************************************************************************
	 	*Obtiene Id del evento
	*/
		EVEIDR = Utilities.EVE_IDR;
	 /********************************************************************************/
	
		
		
		String qry_url = getString(R.string.QRY_URL)
				+ Uri.encode("select * from cintermex");

		saxXML saxparser = new saxXML(qry_url);

		Actividades = saxparser.parse();

		informacion = Actividades.get(0).get("cint_des").toString();
		fotos = Actividades.get(0).get("cint_des").toString();
		ubicacion = Actividades.get(0).get("cint_dir1").toString() + " "
				+ Actividades.get(0).get("cint_dir2").toString() + " "
				+ Actividades.get(0).get("cint_dir3").toString() + " "
				+ Actividades.get(0).get("cint_dir4").toString();
		contacto = Actividades.get(0).get("cint_tel").toString() + " "
				+ Actividades.get(0).get("cint_fax").toString();

		tvInformacion = (TextView) this
				.findViewById(id.tv_informacion_descripcion);
		tvInformacion.setText(informacion.toString().substring(0, 214));

		tvInformacion.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {

				Intent intent = new Intent(Informacion.this, Descripcion.class);

				intent.putExtra("PUTTEXTO", informacion.toString());

				startActivity(intent);

			}
		});

		tvUbicacion = (TextView) this.findViewById(id.tv_informacion_ubicacion);
		tvUbicacion.setText(ubicacion);

		tvContacto = (TextView) this
				.findViewById(id.tv_informacion_telefonocontacto);
		tvContacto.setText(contacto);

		tvContactoMail = (TextView) this
				.findViewById(id.tv_informacion_mailcontacto);
		tvContactoMail.setText(Actividades.get(0).get("cint_mail").toString());

		/** Botones **/

		ImageButton face = (ImageButton) this
				.findViewById(id.iv_informationevento_facebook);

		try {
			final String face_ = Actividades.get(0).get("cint_facebook")
					.toString();
			if (face_.toString().trim().length() > 3) {

				face.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {

						Intent intent = new Intent(Informacion.this, Web.class);
						intent.putExtra("PUTWEB", face_);
						startActivity(intent);

					}
				});
			} else {
				face.setAlpha(80);
			}
		} catch (Exception ex) {
			face.setAlpha(80);
		}

		ImageButton twitter = (ImageButton) this
				.findViewById(id.iv_informationevento_twitter);
		try {
			final String twitter_ = Actividades.get(0).get("cint_twitter")
					.toString();

			if (twitter_.toString().trim().length() > 3) {

				twitter.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {

						Intent intent = new Intent(Informacion.this, Web.class);
						intent.putExtra("PUTWEB", twitter_);
						startActivity(intent);

					}
				});
			} else {
				twitter.setAlpha(80);
			}

		} catch (Exception ex) {
			twitter.setAlpha(80);
		}

		ImageButton link = (ImageButton) this
				.findViewById(id.iv_informationevento_link);
		try {

			final String link_ = Actividades.get(0).get("cint_linkedin")
					.toString();

			if (link_.toString().trim().length() > 3) {

				link.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {

						Intent intent = new Intent(Informacion.this, Web.class);
						intent.putExtra("PUTWEB", link_);
						startActivity(intent);

					}
				});
			} else {
				link.setAlpha(80);
			}
		} catch (Exception ex) {
			link.setAlpha(80);
		}

		ImageButton youtube = (ImageButton) this
				.findViewById(id.iv_informationevento_youtube);

		try {
			final String youtube_ = Actividades.get(0).get("cint_youtube")
					.toString();

			if (youtube_.toString().trim().length() > 3) {

				youtube.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {

						Intent intent = new Intent(Informacion.this, Web.class);
						intent.putExtra("PUTWEB", youtube_);
						startActivity(intent);

					}
				});
			} else {
				youtube.setAlpha(80);
			}
		} catch (Exception ex) {
			youtube.setAlpha(80);
		}

		/** Fin Botones **/

		CargarContenido();

		try {
			ImageView i = (ImageView) findViewById(R.id.img_staticmapa_lugar);
			Bitmap bitmap = BitmapFactory.decodeStream((InputStream) new URL(
					getString(R.string.CINTERMEX_MAP)).getContent());
			i.setImageBitmap(bitmap);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		ImageView llamar = (ImageView) this
				.findViewById(id.iv_informacion_llamar);
		llamar.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				Intent callIntent = new Intent();

				callIntent = new Intent(Intent.ACTION_DIAL,

				Uri.parse("tel:" + tvContacto.getText()));

				startActivity(callIntent);

			}
		});

	}

	private void CargarContenido() {
		// TODO Auto-generated method stub
		new AsyncActividades().execute("contenido");

	}

	private class AsyncActividades extends AsyncTask<String, Void, Integer> {

		protected void onPostExecute(Integer in) {

			gallery1 = (Gallery) findViewById(R.id.galery_fotos);
			myAdapter = new Hoteles_Adapter(Actividades);

			gallery1.setAdapter(myAdapter);

			DisplayMetrics metrics = new DisplayMetrics();
			Informacion.this.getWindowManager().getDefaultDisplay()
					.getMetrics(metrics);

			// Gallery gallery = (Gallery) findViewById(R.id.gallery1);

			MarginLayoutParams mlp = (MarginLayoutParams) gallery1
					.getLayoutParams();
			mlp.setMargins(-(metrics.widthPixels / 2 + 50), mlp.topMargin,
					mlp.rightMargin, mlp.bottomMargin);

			gallery1.setOnItemClickListener(new OnItemClickListener() {
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {

					// Toast.makeText(
					// Calendario.this,
					// gallery1_.get(position).get("eve_nombre")
					// .toString(), Toast.LENGTH_LONG).show();

					// abrirInformacionEvento( Actividades);

					clsImagesShow.setDctionary(Actividades);

					Intent intent = new Intent(Informacion.this, Imagenes.class);
					//
					// clsExpositores.setDctionary(Actividades.get(position));
					//
					startActivity(intent);

				}
			});

		}

		protected void abrirInformacionEvento(Dictionary d) {

			// TODO Auto-generated method stub

		}

		protected void onPreExecute() {

		}

		protected Integer doInBackground(String... urls) {

			String qry_url = getString(R.string.QRY_URL)
					+ Uri.encode("select * from cintermex_fotos");

			saxXML saxparser = new saxXML(qry_url);

			Actividades = saxparser.parse();

			return null;

		}

	}

	public class Hoteles_Adapter extends BaseAdapter {

		private LayoutInflater inflater = null;
		private ArrayList<Dictionary> data;

		public Hoteles_Adapter(ArrayList<Dictionary> d) {

			data = d;
			inflater = (LayoutInflater) Informacion.this
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			//
			imageLoader = new App_ImageLoader(
					Informacion.this.getApplicationContext());

		}

		public int getCount() {
			return data.size();
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public class ViewHolder {

			public ImageView icon;

		}

		public View getView(int position, View convertView, ViewGroup parent) {

			View vi = convertView;
			ViewHolder holder;

			if (convertView == null) {

				vi = inflater.inflate(R.layout.ui_fotos_items, null);

				holder = new ViewHolder();

				holder.icon = (ImageView) vi
						.findViewById(R.id.iv_foto_item_image);

				vi.setTag(holder);

			} else

				holder = (ViewHolder) vi.getTag();

			//
			String url_img = getString(R.string.QRY_URL_IMG)
					+ data.get(position).get("foto_url").toString()
							.replace(" ", "%20");

			holder.icon.setTag(url_img);
			try {

				imageLoader
						.DisplayImage(url_img, Informacion.this, holder.icon);

			} catch (Exception ex) {

				String s = ex.getMessage().toString();

			}

			return vi;

		}

	}

	@Override
	public void onBackPressed() {
	}

	public void abrirDescripcion(View V) {
		Intent intent = new Intent(Informacion.this, Descripcion.class);
		intent.putExtra("PUTTEXTO", informacion);
		startActivity(intent);

	}

	public void voidFinish(View V) {
		finish();

	}

	public void abrirMenuLateral(View V) {
		intent = new Intent(Informacion.this, MenuLateral.class);

		startActivity(intent);
	}
}